﻿using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using QuanLyKhachSan.DTO;
using System.Data.SqlClient;
using System.Data;
using QuanLyKhachSan.DAO;

namespace UnitTest
{
    public class clsTestNguoiDung : clsTest
    {
        private NguoiDungDTO nguoiDung;

        public void Assert_Clauses( DataTable expected,NguoiDungDTO actual )
        {
            if (expected.Rows.Count > 0)
            {
                DataRow dr = expected.Rows[0];
                Assert.AreEqual(dr["tenDangNhap"], actual.TenDangNhap, "I hope the usernames are the same");
                Assert.AreEqual(dr["matKhau"], actual.MatKhau, "Passwords dont match");
                Assert.AreEqual(dr["hoNguoiDung"], actual.HoNguoiDung, "I hope the hoNguoiDung is the same");
                Assert.AreEqual(dr["tenLot"], actual.TenLot, "TenLot not the same");
                Assert.AreEqual(dr["tenNguoiDung"], actual.TenNguoiDung, "TenNguoiDung not the same");
                Assert.AreEqual((int)dr["loaiNguoiDung"], actual.LoaiNguoiDung, "I hope the loaiNguoiDung is the same");
                Assert.AreEqual(dr["CMND"], actual.CMND1, "CMND not the same");
                Assert.AreEqual(dr["diaChi"], actual.DiaChi, "diaChi not the same");
            }
            
        }

        [Test]
        public void TestThongTinNguoiDung()//string userName, string pass)
        {
            string account = "skyhawk133";
            string pass = "";

            nguoiDung = NguoiDungDAO.ThongTinNguoiDung(account, pass);

            Dictionary<string, object> para = new Dictionary<string, object>();
            para.Add("tenDangNhap", account);
            para.Add("matKhau", pass);
            expectedData = select_DataTable("NguoiDung",para);

            Assert_Clauses(expectedData, nguoiDung);

        }

        [Test]
        public void TestThongTinNguoiDung2()//string userName, string pass)
        {
            string account = "nghiep";
            string pass = "e10adc3949ba59abbe56e057f20f883e";


            nguoiDung = NguoiDungDAO.ThongTinNguoiDung(account, pass);

            Dictionary<string, object> para = new Dictionary<string, object>();
            para.Add("tenDangNhap", account);
            para.Add("matKhau", pass);
            expectedData = select_DataTable("NguoiDung", para);

            Assert_Clauses(expectedData, nguoiDung);

        }
        [Test]
        public void TestThongTinNguoiDungMD5()//string userName, string pass)
        {
            string account = "nghiep";
            string pass = "123456";
            pass = NguoiDungDTO.MaHoaMD5(pass);

            nguoiDung = NguoiDungDAO.ThongTinNguoiDung(account, pass);

            Dictionary<string, object> para = new Dictionary<string, object>();
            para.Add("tenDangNhap", account);
            para.Add("matKhau", pass);
            expectedData = select_DataTable("NguoiDung", para);

            Assert_Clauses(expectedData, nguoiDung);
        }
        [Test]
        public void TestInsertNguoiDung()
        {
            NguoiDungDTO nd = new NguoiDungDTO("TestNguoiDung", NguoiDungDTO.MaHoaMD5("nghiệp"), "Nguyễn", "Thị", "Nghiệp", 0, "123454323", "THD");
            bool kq = NguoiDungDAO.Insert(nd);

            Dictionary<string, object> para = new Dictionary<string, object>();
            para.Add("tenDangNhap", nd.TenDangNhap);
            para.Add("matKhau", nd.MatKhau);
            expectedData = select_DataTable("NguoiDung", para);
            if (expectedData.Rows.Count == 0)
            {
                Assert.IsFalse(kq);
            }
            else
            {
                Assert.GreaterOrEqual(expectedData.Rows.Count, 1, "Cần Insert xuống CSDL");
                Assert.IsTrue(kq);
            }
        }
        [Test]
        public void TestDeleteNguoiDung()
        {
            NguoiDungDTO nd = new NguoiDungDTO("TestNguoiDung", NguoiDungDTO.MaHoaMD5("nghiệp"), "Nguyễn", "Thị", "Nghiệp", 0, "123454323", "THD");
            bool kq = NguoiDungDAO.Delete("TestNguoiDung");

            Dictionary<string, object> para = new Dictionary<string, object>();
            para.Add("tenDangNhap", nd.TenDangNhap);
            expectedData = select_DataTable("NguoiDung", para);
            if (expectedData.Rows.Count == 0)
            {
                Assert.IsTrue(kq);
            }
            else
            {
                Assert.GreaterOrEqual(1, expectedData.Rows.Count, "Cần DELETE trong CSDL");
                Assert.IsFalse(kq);
            }

        }
        [Test]
        public void TestMaHoaMD5()
        {
            string test = "abc";
            string kq = NguoiDungDTO.MaHoaMD5(test);
            Assert.AreNotEqual(test, kq, "Can Ma Hoa chuoi");
        }
    }
}
